<?php
class MessageSendException extends WException {}
class Cl_Message extends WdkClass 
{
	protected
		/******************************************************/
		//	원본 테이블 정보
		/******************************************************/
		$message_seq			= 0		,	//	
		$fk_user_seq			= 0		,	//	
		$message_to				= 0		,	//	
		$message_title			= ''	,	//	
		$message_content		= ''	,	//	
		$message_read_flag		= 'F'	,	//	
		$message_read_time		= 0		,	//	
		$message_insert_time	= 0		,	//	
		$message_insert_ip		= ''	,	//	
		$message_del_flag		= 'F'	,	//	
		$message_to_del_flag	= 'F'	,	//	
		/******************************************************/
		//	추가 정보
		/******************************************************/
		$user_nick					= ''	,	//	
		$user_name					= ''	,	//	
		$to_user_nick				= ''	,	//	
		$to_user_name				= ''	,	//	
		$message_insert_time_str	= ''	;	//
	
	/**
	 * 	
	 */
	function __construct()
	{
		parent::__construct();
		self::getDbo();
	}
	
	/**
	 *	
	 */
	public function __init($data)
	{
		parent::__init($data);
		self::dformat();
	}
	
	/**
	 *	
	 */
	public function load($message_seq)
	{
		/*------------------------------------------------------------*/
		$dbo = self::getDbo();
		/*------------------------------------------------------------*/
		self::__init($dbo->cLoad($message_seq));
	}
	
	/**
	 *	
	 */
	public function dformat()
	{
		$this->message_insert_time_str = parent::timeString($this->message_insert_time);
	}
	
	/**
	 * 
	 */
	public function autoUpdate($log='')
	{
		if((int)$this->message_seq > 0)
		{
			$this->update($log);
		}
		else
		{
			$this->insert($log);
		}
	}
	
	/**
	 *	
	 */
	public function insert($log='Insert')
	{
		/*------------------------------------------------------------*/
		$dbo = self::getDbo();
		/*------------------------------------------------------------*/
		
		$this->message_read_flag	= 'F';
		$this->message_read_time	= 0;
		$this->message_insert_time	= time();
		$this->message_del_flag		= 'F';
		$this->message_to_del_flag	= 'F';
		
		
		if($this->message_to == $this->fk_user_seq)
		{
			throw new MessageSendException("본인에게 쪽지를 전송할수 없습니다.");
		}
		$data = array(	'fk_user_seq'			=>	$this->fk_user_seq,
						'message_to'			=>	$this->message_to,
						'message_title'			=>	$this->message_title,
						'message_content'		=>	$this->message_content,
						'message_read_flag'		=>	$this->message_read_flag,
						'message_read_time'		=>	$this->message_read_time,
						'message_insert_time'	=>	$this->message_insert_time,
						'message_insert_ip'		=>	isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '127.0.0.1',
						'message_del_flag'		=>	$this->message_del_flag,
						'message_to_del_flag'	=>	$this->message_to_del_flag
					);
		$this->message_seq = $dbo->insert($data);
		/******************************************************/
		parent::setBackupData(array_keys($data));
		$this->backup($log);
		/******************************************************/
	}
	
	/**
	 * 
	 */
	public function update($log='update')
	{
		/*------------------------------------------------------------*/
		$dbo = self::getDbo();
		/*------------------------------------------------------------*/
		if(parent::isUpdatable())
		{
			$data = array(	'message_title'			=>	$this->message_title,
							'message_content'		=>	$this->message_content
						);
			$dbo->update($data, array("message_seq = ".$this->message_seq));
			/******************************************************/
			parent::setBackupData(array_keys($data));
			$this->backup($log);
			/******************************************************/
		}
	}
	
	/**
	 * 
	 */
	public function send($user_seq=0, $user_login_id='', $title='', $content='')
	{
		
		$user = Wdk::get('User');
		$user->loadByLoginId($user_login_id);
		$this->message_to = $user['user_seq'];
		
		$this->fk_user_seq = $user_seq;
		$this->message_title = $title;
		$this->message_content = $content;
		
		$this->insert();
	}
	
	/**
	 * 
	 */
	public function read($log='read')
	{
		if($this->message_read_flag == 'T') return;
		/*------------------------------------------------------------*/
		$dbo = self::getDbo();
		/*------------------------------------------------------------*/
		$this->message_read_flag	= 'T';
		$this->message_read_time	= time();
		
		$data = array(	'message_read_flag'		=>	$this->message_read_flag,
						'message_read_time'		=>	$this->message_read_time
					);
				
		$dbo->update($data, array("message_seq = ".$this->message_seq));
		/******************************************************/
		parent::setBackupData(array_keys($data));
		$this->backup($log);
		/******************************************************/
	}
	
	/**
	 * 
	 */
	public function cancel($log='cancel')
	{
		/*------------------------------------------------------------*/
		$dbo = self::getDbo();
		/*------------------------------------------------------------*/
		$this->message_del_flag		= 'T';
		$this->message_to_del_flag	= 'T';
		
		$data = array(	'message_del_flag'		=>	$this->message_del_flag,
						'message_to_del_flag'	=>	$this->message_to_del_flag
					);
				
		$dbo->update($data, array("message_seq = ".$this->message_seq));
		/******************************************************/
		parent::setBackupData(array_keys($data));
		$this->backup($log);
		/******************************************************/
	}
	
	/**
	 * 
	 */
	public function delete($log='delete 1 ')
	{
		/*------------------------------------------------------------*/
		$dbo = self::getDbo();
		/*------------------------------------------------------------*/
		$dbo->delete(array("message_seq = ".$this->message_seq));
		/******************************************************/
		parent::setBackupData();
		$this->backup($log);
		/******************************************************/
	}
	
	/**
	 * 
	 */
	public function delete2($log='delete 2')
	{
		/*------------------------------------------------------------*/
		$dbo = self::getDbo();
		/*------------------------------------------------------------*/
		
		$this->message_to_del_flag	= 'T';
		
		$data = array(	'message_to_del_flag'	=>	$this->message_to_del_flag
					);
				
		$dbo->update($data, array("message_seq = ".$this->message_seq));
		/******************************************************/
		parent::setBackupData();
		$this->backup($log);
		/******************************************************/
	}
	
	/**
	 *	
	 */
	private function backup($log='')
	{
		/*------------------------------------------------------------*/
		$dbo = self::getDbo();
		/*------------------------------------------------------------*/
		$dbo->log($this->message_seq, $log, $this->backup_data);
	}
	
	/**
	 * 
	 */
	public function getDbo()
	{
		return Wdk::model(substr(__CLASS__, 3));
	}
}